package com.quanjiaolian.order.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.quanjiaolian.order.service.OrderService;
import com.quanjiaolian.order.serviceimpl.OrderServiceImpl;
import com.quanjiaolian.tools.ResponseToJson;


@SuppressWarnings("serial")
public class OrderSaveAction extends HttpServlet {

	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(OrderSaveAction.class.getName());
	OrderService os = new OrderServiceImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.doPost(request, response);
	}

	/**
	 * 保存用户订单信息
	 */
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			request.setCharacterEncoding("utf-8");
			String Jsonstr = request.getParameter("orderMap");
			Map<String,Object> map = JSONObject.fromObject(Jsonstr);;
			Iterator<String> it = map.keySet().iterator();
			if(map != null && map.size() != 0){
				logger.info("*************已从客户端获得Json订单数据*************");
				while(it.hasNext()){
					String key = it.next();
					logger.info("订单字段名:"+key);
					logger.info("订单字段值:"+map.get(key));
				}
				int i = os.insertOrder(map);
				if(i>0){
					resultMap.put("result","OK");
				}else{
					resultMap.put("result","Failed");
				}
			}else{
				logger.info("*************未从客户端获得Json订单数据*************");
				resultMap.put("result","Error");
			}
		}catch (Exception e) {
			logger.info("保存用户订单信息出错!");
			resultMap.put("result","Error");
			e.printStackTrace();
		}
		
		ResponseToJson.responseOutWithJson(response,resultMap);
		
	}

}
